home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume3 / mmake < prev    next >
Encoding:
Text File  |  1989-02-03  |  8.6 KB  |  265 lines

  1. Path: xanth!mcnc!gatech!bloom-beacon!tut.cis.ohio-state.edu!mandrill!hal!ncoast!allbery
  2. From: eric@cs1.wsu.edu (Eric Schneider)
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i023: mmake - Make for multiple environments
  5. Message-ID: <8805192236.AA25938@XN.LL.MIT.EDU>
  6. Date: 18 May 88 22:31:24 GMT
  7. Sender: usenet@ncoast.UUCP
  8. Reply-To: eric@cs1.wsu.edu (Eric Schneider)
  9. Lines: 253
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. comp.sources.misc: Volume 3, Issue 23
  13. Submitted-By: "Eric Schneider" <eric@cs1.wsu.edu>
  14. Archive-Name: mmake
  15.  
  16. I recently got 'emake' from the archives and upone examining it, noted that
  17. there wasn't a chance in heck of getting it to work under MS-DOS(r).  So, I
  18. spent a few hours coming up with the following program which is quite simple
  19. and runs on Unix, and MS-DOS systems.  For any large program which is in the
  20. process of becoming larger, such as adding and deleting source files, header
  21. files, and the like, and also being cross-developed on UNIX and MS-DOS at the
  22. same time - a tool like this is essential.  If I've re-invented the wheel,
  23. don't hesitate to tell me and point me in some nice direction.  I'm not sure
  24. that my way of doing thing is the best, but the program is quite simple and
  25. does do what I need.
  26.  
  27.  
  28.             Eric (eric@cs1.wsu.edu.CSNET)
  29.  
  30. # :>- :>- :>- CUT HERE :>- :>- :>-
  31. #! /bin/sh
  32. # This is a shell archive, meaning:
  33. # 1. Remove everything above the #! /bin/sh line.
  34. # 2. Save the resulting text in a file.
  35. # 3. Execute the file with /bin/sh (not csh) to create:
  36. #    README
  37. #    mmake.c
  38. # This archive created: Wed May 18 15:19:46 1988
  39. export PATH; PATH=/bin:/usr/bin:$PATH
  40. if test -f 'README'
  41. then
  42.     echo shar: "will not over-write existing file 'README'"
  43. else
  44. cat << \SHAR_EOF > 'README'
  45. Wed May 18 14:32:38 PDT 1988
  46.  
  47. Mmake - Support for Makefiles on multiple operating systems and environments.
  48.  
  49. usage:
  50.     Create a file called "MMakefile", or "mmakefil" on MS-DOS.  This file
  51.     can contain C preprocessor directives such as "#ifdef MSDOS" in order
  52.     to control the make process.  The mmake program is invoked by simply
  53.     typing "mmake <args>".  The arguments to mmake are passed to the C
  54.     preprocessor, or the make program as appropriate.
  55.  
  56. description:
  57.     This program grew out of the need to support simultaneous development
  58.     on the Un*x operating system and MS-D*S.  Because of the nature of the
  59.     C compilers on each system, it was necessary to maintain two seperate
  60.     Makefiles.  This program allows a single makefile to exist which then
  61.     will expand differently depending on the OS in which it is run.  For
  62.     example the following MMakefile might be used to compile with debug
  63.     enabled.
  64.  
  65.         /* Makefile w/ Debug */
  66.  
  67.         #if MSDOS
  68.         DEBUG=-Zi -Od
  69.         PROG=main.exe
  70.         #else
  71.         DEBUG=-g
  72.         PROG=main
  73.         #endif
  74.  
  75.         CFLAGS= $(DEBUG)
  76.  
  77.         $(PROG): main.o
  78.             $(CC) -o $(PROG) $(DEBUG) prog.o
  79.  
  80.     In the msdos environment, the program main.exe will be compiled to
  81.     include symbol table information for the CodeView(r) debugger, and
  82.     in the Unix environment, the standard system debug information will
  83.     be present.
  84.  
  85.  
  86.     To build mmake, just compile the mmake.c file and name the output
  87.     mmake, or mmake.exe if in MS-DOS.  No makefile is provided since
  88.     this is a case where a MMakefile would be nice and you can't mmake
  89.     mmake if mmake doesn't exist!
  90.  
  91.     This program was developed on an HP 9000/840 running HP-UX 1.2.  It
  92.     has also been tested on an IBM AT compatible running PC-DOS 3.3, and
  93.     the MicroSoft C 5.0 compiler.  The make program used on the PC was
  94.     NdMake 4.3.  In addition this program will compile and run under the
  95.     BSD 4.2 or 4.3 operating system.
  96.  
  97.     If there are any questions, or you have any bugs to report or
  98.     enhancements to suggest, don't hesitate to contact me.
  99.  
  100.  
  101.             Eric Schneider, System Manager
  102.             Computer Science Department
  103.             Washington State University
  104.             Pullman, WA  99164-1210
  105.  
  106.             eric@cs1.wsu.edu (CSNET)
  107. SHAR_EOF
  108. fi
  109. if test -f 'mmake.c'
  110. then
  111.     echo shar: "will not over-write existing file 'mmake.c'"
  112. else
  113. cat << \SHAR_EOF > 'mmake.c'
  114. /*****************************************************************************
  115.  *                                                                           *
  116.  * mmake - Program to support make on multiple systems.  This program takes  *
  117.  *         the file MMakefile and runs the C preprocessor on it.  The result *
  118.  *         is written to Makefile, and then "make" is invoked.  Note that    *
  119.  *         the C preprocessor is only invoked if the file MMakefile is newer *
  120.  *         then Makefile.                                                    *
  121.  *                                                                           *
  122.  *         This program is designed to allow the same makefile to be used on *
  123.  *         unix(r) systems and MS-DOS(r) systems.  In order to make this     *
  124.  *         program portable across systems, it is not very smart about the   *
  125.  *         way it does certain things.  For example, in order to run the     *
  126.  *         C preprocessor, we use a system() call, rather than something     *
  127.  *         line popen() which is not available on MS-DOS.                    *
  128.  *                                                                           *
  129.  *         The file names were chosen so that once mmake has run and created *
  130.  *         Makefile, you can run "make" rather than "mmake" unless MMakefile *
  131.  *         changes.                                                          *
  132.  *                                                                           *
  133.  *         Mmake has no arguments, but certain command line arguments are    *
  134.  *         send to either CPP or make as appropriate.  Any argument which    *
  135.  *         begines with "-D", "-I", or "-U" is sent to CPP.  Any other flags *
  136.  *         are passed unchanged to make.                                     *
  137.  *                                                                           *
  138.  *         The output from the C preprocessor is written to a temporary file *
  139.  *         because this output contains blank lines which will cause make to *
  140.  *         detect a syntax error.  The temporary file is read back in and    *
  141.  *         the blank lines are removed with the result goint to Makefile.    *
  142.  *                                                                           *
  143.  *****************************************************************************/
  144.  
  145. #include <stdio.h>
  146. #include <sys/types.h>
  147. #include <sys/stat.h>
  148. #include <signal.h>
  149. #include <errno.h>
  150.  
  151. extern int errno;
  152. int temp_flag;
  153. char *temp_name;
  154.  
  155. main(argc, argv)
  156. int argc;
  157. char **argv;
  158. {
  159.     time_t mmtime, mtime;
  160.     struct stat sb;
  161.     int newer = 0;            /* DOS requires initialization */
  162.     char cpp_command[1024];
  163.     char make_command[1024];
  164.     char *temp_file;
  165.     char line[1024];
  166.     FILE *fpin, *fpout;
  167.     int cleanup();
  168.  
  169.     temp_flag = 0;            /* Indicates that temp file exists */
  170.     signal(SIGINT,cleanup);
  171.  
  172.     if (stat("MMakefile", &sb) < 0) {
  173.     perror("MMakefile");
  174.     exit(1);
  175.     } else
  176.     mmtime = sb.st_mtime;
  177.  
  178.     if (stat("Makefile", &sb) < 0) {
  179.     if (errno == ENOENT)
  180.         mtime = 0;
  181.     else {
  182.         perror("Makefile");
  183.         exit(1);
  184.     }
  185.     } else
  186.     mtime = sb.st_mtime;
  187.  
  188.     strcpy(cpp_command,"cc -E MMakefile ");
  189.     strcpy(make_command, "make ");
  190.  
  191.     if (mmtime > mtime) {        /* do we have to run CPP? */
  192.     newer = 1;
  193.     temp_file = mktemp("MMXXXXXX");
  194.     }
  195.  
  196.     /* Now scan the argument flags and put each in the appropriate
  197.        command line */
  198.     argc--;
  199.     argv++;
  200.     while(*argv) {
  201.     if ((strncmp(*argv,"-D",2) == 0) ||
  202.         (strncmp(*argv,"-U",2) == 0) ||
  203.         (strncmp(*argv,"-I",2) == 0)) {
  204.         strcat(cpp_command, *argv);
  205.         strcat(cpp_command, " ");
  206.     } else {
  207.         strcat(make_command, *argv);
  208.         strcat(make_command, " ");
  209.     }
  210.     argc--;
  211.     argv++;
  212.     }
  213.     strcat(cpp_command, ">");
  214.     if (newer) {
  215.     temp_flag = 1;            /* Say temp file exists */
  216.     temp_name = temp_file;        /* temp file name is temp_name */
  217.     strcat(cpp_command, temp_file);
  218.     } else
  219.     strcat(cpp_command, "Makefile");
  220.  
  221.     if (newer) {
  222.     if (system(cpp_command) != 0) {
  223.         fprintf(stderr, "CPP Failed!\n");
  224.         cleanup();
  225.     }
  226.     if ((fpin = fopen(temp_file, "r")) == NULL) {
  227.         perror(temp_file);
  228.         unlink(temp_file);
  229.         exit(1);
  230.     }
  231.     if ((fpout = fopen("Makefile", "w")) == NULL) {
  232.         perror("Makefile");
  233.         cleanup();
  234.     }
  235.     while (fgets(line, sizeof line, fpin)) {
  236.         if (*line != '\n')
  237.         fputs(line, fpout);
  238.     }
  239.     if (!feof(fpin)) {
  240.         perror("Makefile");
  241.         cleanup();
  242.     }
  243.     fclose(fpin);
  244.     fclose(fpout);
  245.     unlink(temp_file);
  246.     temp_flag = 0;            /* Temp file no longer exists */
  247.     }
  248.  
  249.     return (system(make_command));
  250. }
  251.  
  252. cleanup()
  253. {
  254.     if (temp_flag)
  255.     unlink(temp_name);
  256.     fprintf(stderr, "Mmake aborted.  %s\n", temp_flag ?
  257.         "temporary file removed." : "");
  258.  
  259.     exit(1);
  260. }
  261. SHAR_EOF
  262. fi
  263. exit 0
  264. #    End of shell archive
  265.